package com.example.support.thread;

import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@org.springframework.context.annotation.Configuration
public class ThreadPoolConfiguration {

	@Bean(name = {"taskExecutor", "applicationTaskExecutor"})
	public ThreadPoolTaskExecutor applicationTaskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(1);
		executor.setMaxPoolSize(8);
		executor.setQueueCapacity(10000);
		executor.setThreadNamePrefix("task-pool-");
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
		return executor;
	}

	@Bean(name = "applicationTaskScheduler")
	public ThreadPoolTaskScheduler applicationTaskScheduler() {
		ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
		scheduler.setPoolSize(2);
		scheduler.setThreadNamePrefix("schedule-pool-");
		scheduler.setDaemon(true);
		return scheduler;
	}

}
